﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Prod;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Common;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmManageManufactureOrder : BaseForm.frmBaseMdiBusinessForm
    {
        #region vars
        private List<ProdPlanOrderItemEntity> listSelectedPlanOrderItem;
        private ProdPlanOrderItemEntity mSelectedPlanOrderItem;
        #endregion

        #region ctors
        public frmManageManufactureOrder()
        {
            InitializeComponent();
        }
        public frmManageManufactureOrder(ProdPlanOrderEntity entity)
        {
            InitializeComponent();
            txtProductPlanOrderCode.Text = entity.PlanOrderCode;
        }
        #endregion

        #region functions
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "POI", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(rcolStatus, "POI", false);
        }

        private void SetField()
        {
            cmbStatus.SetSelectedValue("POI01");
        }

        private string GetCondition()
        {
            string sCondition = "";
            if (string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()) == false)
            {
                sCondition += " and ppoi.STATUS='" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            else
            {
                sCondition += " and ppoi.STATUS not in ('POI07','POI09','POI10') ";
            }
            if (string.IsNullOrEmpty(txtProductPlanOrderCode.Text) == false)
            {
                sCondition += " and ppo.PLAN_ORDER_CODE like '%" + txtProductPlanOrderCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtWorkingOrderCode.Text) == false)
            {
                sCondition += " and ppoi.ORDER_ITEM_CODE like '%" + txtWorkingOrderCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtContractModel.Text) == false)
            {
                sCondition += " and sppai.PRODUCT_SPEC_OUT like '%" + txtContractModel.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtProductCode.Text) == false)
            {
                sCondition += " and mp.PRODUCT_CODE like '%" + txtProductCode.Text + "%' ";
            }
            return sCondition;
        }

        private void BindGridMain()
        {
            string sCondition = GetCondition();
            string sOrderBy = "order by ppoi.ORDER_ITEM_CODE desc ";
            DataTable dt = ProdPlanOrderItemBs.GetDtAllByCondition(sCondition, sOrderBy, txtRowCount.Text);

            DataColumn dtCol = new DataColumn();
            dtCol.ColumnName = "IS_SELECTED";
            dtCol.DataType = System.Type.GetType("System.Boolean");
            dtCol.DefaultValue = false;
            dt.Columns.Add(dtCol);

            gvMain.DataSource = dt;
        }

        private void GetManufactureOrderSelected()
        {
            DataTable dt = (DataTable)gvMain.DataSource;
            listSelectedPlanOrderItem = new List<ProdPlanOrderItemEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                if ((bool)dr["IS_SELECTED"] == true)
                {
                    ProdPlanOrderItemEntity m = new ProdPlanOrderItemEntity(dr);
                    listSelectedPlanOrderItem.Add(m);
                }
            }
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            gridView1.OptionsView.ShowGroupPanel = true;
            BindCombobox();
            //SetField();
            BindGridMain();
            gridView1.ExpandAllGroups();
        }

        protected override void Click_Query()
        {
            BindGridMain();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.RowHandle < 0)
                return;
            DataTable dt = (DataTable)gvMain.DataSource;
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPlanOrderItem = new ProdPlanOrderItemEntity(dr);
            lblProdPlanOrder.Text = dr["PLAN_ORDER_CODE"].ToString();
            if ((bool)gridView1.GetDataRow(e.RowHandle)["IS_SELECTED"])
            {
                gridView1.GetDataRow(e.RowHandle)["IS_SELECTED"] = false;
            }
            else
            {
                gridView1.GetDataRow(e.RowHandle)["IS_SELECTED"] = true;
            }
        }

        private void ckAllSelected_CheckedChanged(object sender, EventArgs e)
        {
            if (gvMain.DataSource == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(lblProdPlanOrder.Text))
            {
                return;
            }
            DataTable dt = gvMain.DataSource as DataTable;
            DataRow[] aryDataRow = dt.Select("PLAN_ORDER_CODE='" + lblProdPlanOrder.Text + "'");
            if (ckAllSelected.Checked == true)
            {
                foreach (DataRow dr in aryDataRow)
                {
                    dr["IS_SELECTED"] = true;
                }
            }
            else
            {
                foreach (DataRow dr in aryDataRow)
                {
                    dr["IS_SELECTED"] = false;
                }
            }
        }

        private void btnBlueprintManage_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)gvMain.DataSource;
            listSelectedPlanOrderItem = new List<ProdPlanOrderItemEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                if ((bool)dr["IS_SELECTED"] == true)
                {
                    ProdPlanOrderItemEntity m = new ProdPlanOrderItemEntity(dr);
                    listSelectedPlanOrderItem.Add(m);
                }
            }
            frmManageBlueprintPrint frm = new frmManageBlueprintPrint();
            frm.ListProdPlanOrderItem = listSelectedPlanOrderItem;
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void btnSetTechnic_Click(object sender, EventArgs e)
        {
            GetManufactureOrderSelected();
            if (listSelectedPlanOrderItem.Count != 1)
            {
                MessageBox.Show("请选择一张工令单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainManufactureTechnic frm = new frmMaintainManufactureTechnic(listSelectedPlanOrderItem[0]);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridMain();
            }
        }

        private void btnWorkingOrderPrint_Click(object sender, EventArgs e)
        {
            GetManufactureOrderSelected();
            if (listSelectedPlanOrderItem.Count != 1)
            {
                MessageBox.Show("请选择一张工令单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = ProdProductTechnicBs.GetDtItemFlowStationByItemID(listSelectedPlanOrderItem[0].PlanOrderItemId);
            if (dt == null || dt.Rows.Count <= 0)
            {
                MessageBox.Show("该工令单尚未排定工艺流程！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmPrintFormWorkingOrder frm = new frmPrintFormWorkingOrder(listSelectedPlanOrderItem[0]);
            frm.ShowDialog();
        }

        private void btnPicking_Click(object sender, EventArgs e)
        {
            GetManufactureOrderSelected();
            if (listSelectedPlanOrderItem.Count == 0)
            {
                MessageBox.Show("请选择一张或多张工令单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPickingPlan frm = new frmMaintainPickingPlan(listSelectedPlanOrderItem);
            frm.ThisActionType = ActionType.New;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridMain();
            }
        }

        private void btnPickingManage_Click(object sender, EventArgs e)
        {
            frmManagePickingPlan frm = new frmManagePickingPlan();
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void btnOutsourcingManage_Click(object sender, EventArgs e)
        {
            frmManageCooperation frm = new frmManageCooperation();
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            GetManufactureOrderSelected();
            if (listSelectedPlanOrderItem.Count != 1)
            {
                MessageBox.Show("请选择一张工令单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainReportWorking frm = new frmMaintainReportWorking(listSelectedPlanOrderItem[0]);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridMain();
            }
        }

        private void btnRework_Click(object sender, EventArgs e)
        {
            GetManufactureOrderSelected();
            if (listSelectedPlanOrderItem.Count != 1)
            {
                MessageBox.Show("请选择一张工令单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainReworkOrder frm = new frmMaintainReworkOrder(listSelectedPlanOrderItem[0]);
            frm.ThisActionType = ActionType.New;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridMain();
            }
        }

        private void tsmPause_Click(object sender, EventArgs e)
        {
            DataRow dr = gridView1.GetFocusedDataRow();
            mSelectedPlanOrderItem = new ProdPlanOrderItemEntity(dr);
            if (mSelectedPlanOrderItem == null)
            {
                MessageBox.Show("请选择一条工令单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (MessageBox.Show("确定暂停工令单：" + mSelectedPlanOrderItem.OrderItemCode + "？", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == DialogResult.Yes)
            {
                mSelectedPlanOrderItem.Status = "POI11";
                if (ProdPlanOrderItemBs.UpdateProdPlanOrderItem(mSelectedPlanOrderItem))
                {
                    MessageBox.Show("工令单：" + mSelectedPlanOrderItem.OrderItemCode + "已暂停！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindGridMain();
                }
            }
        }

        private void tsmClose_Click(object sender, EventArgs e)
        {
            DataRow dr = gridView1.GetFocusedDataRow();
            mSelectedPlanOrderItem = new ProdPlanOrderItemEntity(dr);
            if (mSelectedPlanOrderItem == null)
            {
                MessageBox.Show("请选择一条工令单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (MessageBox.Show("确定关闭工令单：" + mSelectedPlanOrderItem.OrderItemCode + "？", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == DialogResult.Yes)
            {
                mSelectedPlanOrderItem.Status = "POI10";
                if (ProdPlanOrderItemBs.UpdateProdPlanOrderItem(mSelectedPlanOrderItem))
                {
                    MessageBox.Show("工令单：" + mSelectedPlanOrderItem.OrderItemCode + "已关闭！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindGridMain();
                }
            }
        }

        private void tsmRefresh_Click(object sender, EventArgs e)
        {
            DataRow dr = gridView1.GetFocusedDataRow();
            if (dr == null)
                return;
            mSelectedPlanOrderItem = new ProdPlanOrderItemEntity(dr);
            if (mSelectedPlanOrderItem == null)
                return;
            string sCondition = "and ppoi.plan_order_item_id='"+mSelectedPlanOrderItem.PlanOrderItemId+"' ";
            DataTable dt = ProdPlanOrderItemBs.GetDtAllByCondition(sCondition, "", "");
            if (dt == null)
                return;
            if (dt.Rows.Count == 0)
                return;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                dr[i] = dt.Rows[0][i];
            }
            mSelectedPlanOrderItem = new ProdPlanOrderItemEntity(dr);
        }

        private void repositoryItemHyperLinkEdit1_Click(object sender, EventArgs e)
        {
            try
            {
                DevExpress.XtraEditors.HyperLinkEdit link = (DevExpress.XtraEditors.HyperLinkEdit)sender;
                string sProductCode = link.Text;
                EsolWevServices.EsolService ws = new EsolWevServices.EsolService();
                byte[] arrFileData = ws.DownloadFile(sProductCode + ".jpg");
                Image img = CommonFunctions.byteArrayToImage(arrFileData);
                CommonFunctions.OpenPicViewer(img);
            }
            catch (Exception ex)
            {
                MessageBox.Show("图片不存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void repositoryItemCheckEdit1_Click(object sender, EventArgs e)
        {
            DataRow dr = gridView1.GetFocusedDataRow();
            mSelectedPlanOrderItem = new ProdPlanOrderItemEntity(dr);
            lblProdPlanOrder.Text = dr["PLAN_ORDER_CODE"].ToString();
            if ((bool)dr["IS_SELECTED"])
            {
                dr["IS_SELECTED"] = false;
                ((DevExpress.XtraEditors.CheckEdit)sender).Checked = false;
            }
            else
            {
                dr["IS_SELECTED"] = true;
                ((DevExpress.XtraEditors.CheckEdit)sender).Checked = true;
            }

        }

        #endregion

        private void btnExport_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(sfd.FileName);
            }
        }

    }
}
